/*
 * Created by JFormDesigner on Mon Jul 01 15:43:15 CST 2024
 */

package scct.yzx.ui;

import scct.yzx.dao.ConversationDao;
import scct.yzx.model.Conversation;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author lenovo
 */
public class PanelConversation extends JPanel {
    DefaultTableModel Searchmodel = null;
    DefaultTableModel model;
    FormMain main;
    public PanelConversation(FormMain main) {
        this.main=main;
        initComponents();
        initView();
    }

    private void initView() {
        //        自己的初始化
//        获取表格对象的数据类型，通过对模型的操作，可以为表格和显示数据
        
        model=(DefaultTableModel) tabConversation.getModel();
        model.addColumn("id");
        model.addColumn("用户名");
        model.addColumn("联系人");
        model.addColumn("最后会话时间");
        getTable();
    }

    private void getTable() {
        model.setRowCount(0);
        ConversationDao conversationDao=new ConversationDao();
        List<Conversation> AllConversation=conversationDao.getAllConversation();
        for (Conversation conversation:AllConversation){
            model.addRow(new Object[]{conversation.getId(),conversation.getUsername(),conversation.getContactname(),conversation.getLasttime()});
        }
    }


    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        DlgConversationAdd dlgConversationAdd=new DlgConversationAdd(main);
        dlgConversationAdd.setModal(true);
        dlgConversationAdd.setVisible(true);
        getTable();
    }

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here

        if (tabConversation.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择要修改的会话");
            return;
        }
        int index = tabConversation.getSelectedRow();
        int id = Integer.parseInt(model.getValueAt(index,0).toString());
        System.out.println(id);
        DlgConversationUpdate dlgConversationUpdate=new DlgConversationUpdate(main,id);
        dlgConversationUpdate.setModal(true);
        dlgConversationUpdate.setVisible(true);
        getTable();

       
    }

    private void btnDelete(ActionEvent e) {
        // TODO add your code here
        if (tabConversation.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择要删除的会话");
            return;
        }
        int index = tabConversation.getSelectedRow();
        int id = Integer.parseInt(model.getValueAt(index,0).toString());
        System.out.println(id);
        DlgConversationDelete dlgConversationDelete=new DlgConversationDelete(main,id);
        dlgConversationDelete.setModal(true);
        dlgConversationDelete.setVisible(true);

        getTable();
    
    }

    private void btnSearch(ActionEvent e) {
        // TODO add your code here

        System.out.println(e.getSource());
        tabConversation.setModel(model);
        Searchmodel = new DefaultTableModel();
        Searchmodel.addColumn("id");
        Searchmodel.addColumn("用户名");
        Searchmodel.addColumn("联系人");
        Searchmodel.addColumn("最后会话时间");
        Searchmodel.setRowCount(0);
        String searchkey = txtSearch.getText();
        System.out.println(searchkey);
        String searchList = (String) comSearch.getSelectedItem();
        System.out.println(searchList);
        int row = tabConversation.getRowCount();
        System.out.println("row:" + row);
        int column = tabConversation.getColumnCount();
        System.out.println("column:" + column);
        for (int i = 0; i < row; i++) {
            if (tabConversation.getRowCount() < row){
            }else {
                for (int j = 0; j < column; j++) {


                    if (Objects.equals(searchList, tabConversation.getColumnName(j))) {
                        System.out.println("单元格的值：" + tabConversation.getValueAt(i, j));
                        String cellValue = tabConversation.getValueAt(i, j).toString().toLowerCase();
                        System.out.println("cellValue:" + cellValue);
                        if (cellValue.contains(searchkey)) {
                            Object[] rowDate = new Object[tabConversation.getColumnCount()];
                            for (int k = 0; k < tabConversation.getColumnCount(); k++) {
                                rowDate[k] = tabConversation.getValueAt(i, k);
                                System.out.println(rowDate[k]);
                            }
                            Searchmodel.addRow(rowDate);
                            tabConversation.setRowSelectionAllowed(true);
                            break;
                        }

                    }
                }
            }
        }
        tabConversation.setModel(Searchmodel);
    }






    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhang
        panel1 = new JPanel();
        comSearch = new JComboBox<>();
        txtSearch = new JTextField();
        btnSearch = new JButton();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        btnDelete = new JButton();
        scrollPane1 = new JScrollPane();
        tabConversation = new JTable();

        //======== this ========
        setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax . swing. border .EmptyBorder
        ( 0, 0 ,0 , 0) ,  "JFor\u006dDesi\u0067ner \u0045valu\u0061tion" , javax. swing .border . TitledBorder. CENTER ,javax . swing. border
        .TitledBorder . BOTTOM, new Font ( "Dia\u006cog", Font. BOLD ,12 ) , Color .red ) , getBorder () ) );  addPropertyChangeListener(new java. beans .PropertyChangeListener ( ){ @Override public void
        propertyChange (java . beans. PropertyChangeEvent e) { if( "bord\u0065r" .equals ( e. getPropertyName () ) )throw new RuntimeException( )
        ;} } );
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- comSearch ----
            comSearch.setModel(new DefaultComboBoxModel<>(new String[] {
                "id",
                "\u7528\u6237\u540d",
                "\u8054\u7cfb\u4eba",
                "\u6700\u540e\u4f1a\u8bdd\u65f6\u95f4"
            }));
            panel1.add(comSearch);
            comSearch.setBounds(new Rectangle(new Point(5, 5), comSearch.getPreferredSize()));
            panel1.add(txtSearch);
            txtSearch.setBounds(110, 5, 85, txtSearch.getPreferredSize().height);

            //---- btnSearch ----
            btnSearch.setText("\u641c\u7d22");
            btnSearch.addActionListener(e -> btnSearch(e));
            panel1.add(btnSearch);
            btnSearch.setBounds(new Rectangle(new Point(200, 5), btnSearch.getPreferredSize()));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);
            btnAdd.setBounds(new Rectangle(new Point(331, 5), btnAdd.getPreferredSize()));

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);
            btnUpdate.setBounds(new Rectangle(new Point(414, 5), btnUpdate.getPreferredSize()));

            //---- btnDelete ----
            btnDelete.setText("\u5220\u9664");
            btnDelete.addActionListener(e -> btnDelete(e));
            panel1.add(btnDelete);
            btnDelete.setBounds(new Rectangle(new Point(497, 5), btnDelete.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabConversation);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhang
    private JPanel panel1;
    private JComboBox<String> comSearch;
    private JTextField txtSearch;
    private JButton btnSearch;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JButton btnDelete;
    private JScrollPane scrollPane1;
    private JTable tabConversation;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
